package com.bamboo.servicebase.exceptionhandler;

import com.bamboo.commonutils.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author jack.wu
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 指定出现什么异常执行这个方法
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity error(Exception e) {
        e.printStackTrace();
        return ResponseEntity.error().message("执行了全局异常处理..");
    }

    /**
     * 特定异常
     */
    @ExceptionHandler(ArithmeticException.class)
    public ResponseEntity error(ArithmeticException e) {
        e.printStackTrace();
        return ResponseEntity.error().message("执行了ArithmeticException异常处理..");
    }

    /**
     * 自定义异常
     */
    @ExceptionHandler(BambooException.class)
    public ResponseEntity error(BambooException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseEntity.error().code(e.getCode()).message(e.getMsg());
    }

}
